package com.jhgsys.internal.peccancy.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.peccancy.entity.PeccancyInformationFile;
import com.jhgsys.internal.peccancy.service.IPeccancyInformationFileService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 * 违章图片 Controller
 *
 * @author wanghw
 * @date 2024-08-12 09:46:26
 */
@Slf4j
@Validated
@Controller
public class PeccancyInformationFileController extends BaseController {

    @Autowired
    private IPeccancyInformationFileService peccancyInformationFileService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "peccancyInformationFile")
    @RequiresPermissions("peccancyInformationFile:view")
    public String peccancyInformationFileIndex(){
        return jumpPage("peccancy/peccancyInformationFile/peccancyInformationFileList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "peccancyInformationFile/add")
    @RequiresPermissions("peccancyInformationFile:add")
    public String peccancyInformationFileAdd(){
        return jumpPage("peccancy/peccancyInformationFile/peccancyInformationFileEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "peccancyInformationFile/update/{id}")
    @RequiresPermissions("peccancyInformationFile:edit")
    public String peccancyInformationFileUpdate(Model model, @PathVariable("id") Integer id){
        model.addAttribute("peccancyInformationFile", peccancyInformationFileService.findPeccancyInformationFileById(id));
        return jumpPage("peccancy/peccancyInformationFile/peccancyInformationFileEdit");
    }

    /**
     * 查询（所有）
     * @author wanghw
     * @date 2024-08-12 09:46:26
     */
    @GetMapping("peccancyInformationFile")
    @ResponseBody
    @RequiresPermissions("peccancyInformationFile:view")
    public FebsResponse getAllPeccancyInformationFiles(PeccancyInformationFile peccancyInformationFile) {
        return new FebsResponse().success().data(peccancyInformationFileService.findPeccancyInformationFileList(peccancyInformationFile));
    }

    /**
     * 查询（分页）
     * @author wanghw
     * @date 2024-08-12 09:46:26
     */
    @GetMapping("peccancyInformationFile/list")
    @ResponseBody
    @RequiresPermissions("peccancyInformationFile:view")
    public FebsResponse peccancyInformationFileList(QueryRequest request, PeccancyInformationFile peccancyInformationFile) {
        Map<String, Object> dataTable = getDataTable(this.peccancyInformationFileService.findPeccancyInformationFilePage(request, peccancyInformationFile));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 PeccancyInformationFile
     * @author wanghw
     * @date 2024-08-12 09:46:26
     */
    @Log("新增PeccancyInformationFile")
    @PostMapping("peccancyInformationFile")
    @ResponseBody
    @RequiresPermissions("peccancyInformationFile:add")
    public FebsResponse addPeccancyInformationFile(@Valid PeccancyInformationFile peccancyInformationFile) throws FebsException {
        try {
            peccancyInformationFile.setCreatedBy(getCurrentUser().getUserName());
            this.peccancyInformationFileService.createPeccancyInformationFile(peccancyInformationFile);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增PeccancyInformationFile失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 逻辑删除 PeccancyInformationFile
     * @author wanghw
     * @date 2024-08-12 09:46:26
     */
    @Log("逻辑删除PeccancyInformationFile")
    @GetMapping("peccancyInformationFile/delete/{id}")
    @ResponseBody
    @RequiresPermissions("peccancyInformationFile:delete")
    public FebsResponse deletePeccancyInformationFile(@PathVariable("id") String peccancyInformationFileIds) throws FebsException {
        try {
            PeccancyInformationFile peccancyInformationFile = new PeccancyInformationFile();
            peccancyInformationFile.setDeletedBy(getCurrentUser().getUserName());
            peccancyInformationFile.setDeletedTime(new Date());
            peccancyInformationFile.setDeletedFlg(FebsConstant.INVALID);
            this.peccancyInformationFileService.deletePeccancyInformationFile(peccancyInformationFile,peccancyInformationFileIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除PeccancyInformationFile失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
    * 物理删除 PeccancyInformationFile
    * @author wanghw
    * @date 2024-08-12 09:46:26
    */
    @Log("物理删除PeccancyInformationFile")
    @GetMapping("peccancyInformationFile/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("peccancyInformationFile:delete")
    public FebsResponse physicallyDeletePeccancyInformationFile(@PathVariable("id") String peccancyInformationFileIds) throws FebsException {
        try {
            this.peccancyInformationFileService.physicallyDeletePeccancyInformationFile(peccancyInformationFileIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除PeccancyInformationFile失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 修改 PeccancyInformationFile
     * @author wanghw
     * @date 2024-08-12 09:46:26
     */
    @Log("修改PeccancyInformationFile")
    @PostMapping("peccancyInformationFile/update")
    @ResponseBody
    @RequiresPermissions("peccancyInformationFile:edit")
    public FebsResponse updatePeccancyInformationFile(PeccancyInformationFile peccancyInformationFile) throws FebsException {
        try {
            peccancyInformationFile.setUpdatedBy(getCurrentUser().getUserName());
            this.peccancyInformationFileService.updatePeccancyInformationFile(peccancyInformationFile);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改PeccancyInformationFile失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 导出 PeccancyInformationFile的excel
     * @author wanghw
     * @date 2024-08-12 09:46:26
     */
    @PostMapping("peccancyInformationFile/excel")
    @ResponseBody
    @RequiresPermissions("peccancyInformationFile:view")
    public void export(QueryRequest queryRequest, PeccancyInformationFile peccancyInformationFile, HttpServletResponse response) throws FebsException {
        try {
            List<PeccancyInformationFile> peccancyInformationFiles = this.peccancyInformationFileService.findPeccancyInformationFilePage(queryRequest, peccancyInformationFile).getRecords();
            ExcelKit.$Export(PeccancyInformationFile.class, response).downXlsx(peccancyInformationFiles, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
}
